#include "level.h"

BITMAP *Level::Tileset = NULL;

Level::~Level()
{
    destroy_bitmap(Tileset);
}

Level::Level(char* pFilename)
{
    if (Tileset == NULL)
    {
        Tileset = load_bitmap("gfx/tiles.bmp", NULL);
    }

    BITMAP *levelImg = load_bitmap(pFilename, NULL);

    isReady = true;

    if (levelImg == NULL) { isReady = false; allegro_message("WAHAT"); }

    if (levelImg->w < 20 || levelImg->h < 15) { allegro_message("DAMNIT"); isReady = false; }

    if (isReady == true)
    {
        for (int loopX = 0; loopX < 20; ++loopX)
        {
            for (int loopY = 0; loopY < 15; ++loopY)
            {
                if (getpixel(levelImg, loopX, loopY) == -1)
                {
                    return;
                }
                if (getpixel(levelImg, loopX, loopY) == makecol(0, 0, 0)) //Wall tile
                {
                    LevelArray[loopX][loopY].isSolid = false;
                    LevelArray[loopX][loopY].tileType = TILE_FLOOR;
                }
                if (getpixel(levelImg, loopX, loopY) == makecol(255,255,255)) //Wall tile
                {
                    LevelArray[loopX][loopY].isSolid = true;
                    LevelArray[loopX][loopY].tileType = TILE_WALL;
                }
                if (getpixel(levelImg, loopX, loopY) == makecol(0, 200, 200)) //Cooler, wall on right
                {
                    LevelArray[loopX][loopY].isSolid = false;
                    LevelArray[loopX][loopY].tileType = TILE_RCOOLER;
                }
                if (getpixel(levelImg, loopX, loopY) == makecol(100, 200, 200)) //Cooler, wall on left
                {
                    LevelArray[loopX][loopY].isSolid = false;
                    LevelArray[loopX][loopY].tileType = TILE_LCOOLER;
                }
                if (getpixel(levelImg, loopX, loopY) == makecol(128, 64, 0)) //Workbench pointing left
                {
                    LevelArray[loopX][loopY].isSolid = false;
                    LevelArray[loopX][loopY].tileType = TILE_LWORK;
                }
                if (getpixel(levelImg, loopX, loopY) == makecol(128, 64, 64)) //Workbench pointing right
                {
                    LevelArray[loopX][loopY].isSolid = false;
                    LevelArray[loopX][loopY].tileType = TILE_RWORK;
                }
                if (getpixel(levelImg, loopX, loopY) == makecol(255, 64, 0)) //Second Workbench pointing left
                {
                    LevelArray[loopX][loopY].isSolid = false;
                    LevelArray[loopX][loopY].tileType = TILE_SLWORK;
                }
                if (getpixel(levelImg, loopX, loopY) == makecol(255, 128, 0)) //Second Workbench pointing right
                {
                    LevelArray[loopX][loopY].isSolid = false;
                    LevelArray[loopX][loopY].tileType = TILE_SRWORK;
                }
            }
        }
    }

}

int Level::drawTiles()
{
    if (Tileset == NULL)
    {
        Tileset = load_bitmap("gfx/tiles.bmp", NULL);
    }

    for (int rowX = 0; rowX < 20; ++rowX)
    {
        for (int rowY = 0; rowY < 15; ++rowY)
        {
            if (LevelArray[rowX][rowY].tileType == TILE_WALL)
            {
                masked_blit(Tileset, Globals.BUFFER, 16, 0, rowX*16, rowY*16, 16, 16);
            }

            if (LevelArray[rowX][rowY].tileType == TILE_RCOOLER)
            {
                masked_blit(Tileset, Globals.BUFFER, 32, 16, rowX*16, rowY*16, 16, 16);
            }
            if (LevelArray[rowX][rowY].tileType == TILE_LCOOLER)
            {
                masked_blit(Tileset, Globals.BUFFER, 48, 16, rowX*16, rowY*16, 16, 16);
            }
            if (LevelArray[rowX][rowY].tileType == TILE_LWORK)
            {
                masked_blit(Tileset, Globals.BUFFER, 32, 0, rowX*16, rowY*16, 16, 16);
            }
            if (LevelArray[rowX][rowY].tileType == TILE_RWORK)
            {
                masked_blit(Tileset, Globals.BUFFER, 48, 0, rowX*16, rowY*16, 16, 16);
            }
            if (LevelArray[rowX][rowY].tileType == TILE_SLWORK)
            {
                masked_blit(Tileset, Globals.BUFFER, 112, 0, rowX*16, rowY*16, 16, 16);
            }
            if (LevelArray[rowX][rowY].tileType == TILE_SRWORK)
            {
                masked_blit(Tileset, Globals.BUFFER, 128, 0, rowX*16, rowY*16, 16, 16);
            }

        }
    }

    return 0;
}

int Level::drawShadows()
{
    if (Tileset == NULL)
    {
        Tileset = load_bitmap("gfx/tiles.bmp", NULL);
    }

    for (int rowX = 0; rowX < 20; ++rowX)
    {
        for (int rowY = 0; rowY < 15; ++rowY)
        {
            if (LevelArray[rowX][rowY].tileType == TILE_WALL)
            {
                masked_blit(Tileset, Globals.BUFFER, 16, 16, rowX*16, rowY*16 + 16, 16, 16);
            }

            if (LevelArray[rowX][rowY].tileType == TILE_LWORK)
            {
                masked_blit(Tileset, Globals.BUFFER, 16, 16, rowX*16, rowY*16 + 16, 16, 16);
            }
            if (LevelArray[rowX][rowY].tileType == TILE_RWORK)
            {
                masked_blit(Tileset, Globals.BUFFER, 16, 16, rowX*16, rowY*16 + 16, 16, 16);
            }
            if (LevelArray[rowX][rowY].tileType == TILE_SLWORK)
            {
                masked_blit(Tileset, Globals.BUFFER, 16, 16, rowX*16, rowY*16 + 16, 16, 16);
            }
            if (LevelArray[rowX][rowY].tileType == TILE_SRWORK)
            {
                masked_blit(Tileset, Globals.BUFFER, 16, 16, rowX*16, rowY*16 + 16, 16, 16);
            }

        }
    }
    return 0;
}

bool Level::isSolid(int x, int y)
{
    x /= 16;
    y /= 16;

    if (LevelArray[x][y].isSolid)
    {
        return true;
    }

    return false;
}

bool Level::isLCooler(int x, int y)
{
    x /= 16;
    y /= 16;

    if (LevelArray[x][y].tileType == TILE_LCOOLER)
    {
        return true;
    }

    return false;
}

bool Level::isRCooler(int x, int y)
{
    x /= 16;
    y /= 16;

    if (LevelArray[x][y].tileType == TILE_RCOOLER)
    {
        return true;
    }

    return false;
}
